<script setup lang="ts">
// import { useMobileCode } from '@/composables'
// import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { loginPassword, register } from '@/services/user'
import { codeRules, userNameRules, passwordRules } from '@/utils/rules'
import { type FormInstance, showSuccessToast, showToast } from 'vant'
// import { showSuccessToast, showToast } from 'vant'
import { onUnmounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const username = ref('user')
const password = ref('1234')
const agree = ref(false)

const store = useUserStore()
const router = useRouter()
const route = useRoute()
// 注册与登录界面切换
const isPass = ref(true)
const registerCode = ref('')
const onSubmit = async () => {
  if (!agree.value) return showToast('请勾选协议')
  const res = isPass.value
    ? await loginPassword(username.value, password.value)
    : await register(username.value, registerCode.value)
  store.setUser(res.data)
  showSuccessToast(isPass.value ? '登录成功' : '注册成功')
  router.push((route.query.returnUrl as string) || '/home')
}

// 发送短信验证码
// const { onSend, time, form } = useMobileCode(mobile)

// 密码的可见与不可见
const isShow = ref(false)

// http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback

const form = ref<FormInstance>()

onUnmounted(() => {})
</script>

<template>
  <div class="login-page">
    <!-- <cp-nav-bar></cp-nav-bar> -->
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '登录' : '注册' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">
          {{ isPass ? '注册' : '登录' }}
        </span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" ref="form" @submit="onSubmit">
      <van-field
        name="mobile"
        v-model="username"
        placeholder="请输入账号"
        type="tel"
        :rules="userNameRules"
      ></van-field>
      <van-field
        v-if="isPass"
        v-model="password"
        placeholder="请输入密码"
        :type="isShow ? 'text' : 'password'"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${isShow ? 'on' : 'off'}`"
            @click="isShow = !isShow"
            style="margin-right: 10px"
          ></cp-icon>
        </template>
      </van-field>
      <van-field v-else placeholder="请输入密码" v-model="registerCode" :rules="codeRules">
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary"> 登 录 </van-button>
      </div>
      <!-- <div class="cp-cell">
        <a href="javascript:;" @click.prevent="$router.push('/forgetPas')">忘记密码？</a>
      </div> -->
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  border: 1px solid var(--border-color);
  border-radius: 5px;
  background-color: var(--bg-secondary);
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    background-color: var(--bg-tertiary);
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
